import 'package:english_words/english_words.dart';
import 'package:fish_redux/fish_redux.dart';
import 'package:wowhy/pages/home_page/state.dart';
import 'package:wowhy/pages/home_page/word_pair_component/component.dart';

import 'reducer.dart';

class HomeListAdapter extends DynamicFlowAdapter<HomePageState> {
  HomeListAdapter()
      : super(
          pool: <String, Component<Object>>{
            'word_pair': new WordPairComponent()
          },
          connector: _ListConnector(),
          reducer: buildReducer(),
        );
}

class _ListConnector extends ConnOp<HomePageState, List<ItemBean>> {
  @override
  List<ItemBean> get(HomePageState state) {
    if (state.words?.isNotEmpty == true) {
      return state.words
          .map<ItemBean>((WordPair word) => ItemBean('word_pair', word))
          .toList(growable: true);
    }

    return <ItemBean>[];
  }

  @override
  void set(HomePageState state, List<ItemBean> items) {
    if (items?.isNotEmpty == true) {
      state.words = List<WordPair>.from(
        items.map<WordPair>((ItemBean item) => item.data).toList(),
      );
    } else {
      state.words = [];
    }
  }
}
